package com.ruoyi.iot.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.iot.mapper.ChannelMapper;
import com.ruoyi.iot.mapper.PropertyMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.iot.domain.Channel;
import com.ruoyi.iot.domain.Property;
import com.ruoyi.iot.domain.emun.ChannelConstant;
import com.ruoyi.iot.domain.emun.ProductConstant;
import com.ruoyi.iot.service.IChannelService;

/**
 * 产品通道Service业务层处理
 * 
 * @author 常康
 * @date 2025-04-03
 */
@Service
public class ChannelServiceImpl implements IChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private PropertyMapper propertyMapper;

    /**
     * 查询产品通道
     * 
     * @param id 产品通道主键
     * @return 产品通道
     */
    @Override
    public Channel selectChannelById(Long id){
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询产品通道列表
     * 
     * @param channel 产品通道
     * @return 产品通道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel){
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增产品通道
     * 
     * @param channel 产品通道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel){
        Channel oldChannel = channelMapper.selectChannelByProductIdAndKey(channel.getProductId(), channel.getKey());
        if (oldChannel != null) {
            throw new ServiceException("通道key已存在");
        }
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改产品通道
     * 
     * @param channel 产品通道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel){
        Channel oldChannel = channelMapper.selectChannelByProductIdAndKey(channel.getProductId(), channel.getKey());
        if (oldChannel != null && !oldChannel.getId().equals(channel.getId()) ) {
            throw new ServiceException("通道key已存在");
        }
        // 通道key不允许修改
        channel.setKey(null);
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除产品通道
     * 
     * @param ids 需要删除的产品通道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids){
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除产品通道信息
     * 
     * @param id 产品通道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id){
        Channel channel = selectChannelById(id);
        if(ChannelConstant.DEFAULT_CHANNEL_KEY.equals(channel.getKey())){
            throw new ServiceException("默认通道无法删除");
        }
        Property property = new Property();
        property.setChannelId(id);
        List<Property> propertyList = propertyMapper.selectPropertyList(property);
        if(propertyList!=null && !propertyList.isEmpty()){
            throw new ServiceException("请先删除该通道下的属性");
        }
        return channelMapper.deleteChannelById(id);
    }

    @Override
    public Channel buildDefaultChannel(Long productId, String productKey) {
        //添加默认上行通道
        Channel channel = new Channel();
        channel.setProductId(productId);
        channel.setName(ProductConstant.DEFAULT_UP_CHANNEL_NAME);
        channel.setKey(ChannelConstant.DEFAULT_CHANNEL_KEY);
        channel.setAck(ChannelConstant.IS_ACK);
        channel.setProductKey(productKey);
        insertChannel(channel);
        return channel;
    }


}
